<?php
require "common.php";

$username = $_POST['username'];
$password = $_POST['password'];

if (empty($username) || empty($password)) {
    echo jsonReturn(2, '用户名或密码不能为空');
    exit;
}

// 判断是否已经注册过
$sql = "select * from user where username = $username";

$query = mysqli_query($connect, $sql);

$user = mysqli_fetch_assoc($query);

if (!empty($user)) {
    echo jsonReturn(2, '用户名已存在');
    exit;
}

$password = md5($password);
$time = time();

$addSql = "insert into user values (null,$username, '$password', $time)";

$addQuery = mysqli_query($connect, $addSql);

if ($addQuery) {
    echo jsonReturn(1, '注册成功');
} else {
    echo jsonReturn(2, '注册失败，请稍后再试');
}